package com.controller;

import com.webserver.annotations.Controller;
import com.webserver.annotations.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import qrcode.QRCodeUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

@Controller
public class ToolController {

    @RequestMapping("/myweb/random")
    public void createRandomImage(HttpServletRequest request, HttpServletResponse response){
        //1创建一张图片，指定宽高
        BufferedImage image = new BufferedImage(70,30, BufferedImage.TYPE_INT_RGB);
        //2从图片上获取一个画笔，目的是通过这个画笔往图片上画东西
        Graphics g = image.getGraphics();
        //3创建一个Random用于产生随机数
        Random random = new Random();
        //图片上允许出现的字符范围(英文和数字)
        String line = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        //随机颜色做背景
        Color bgcolor = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
        g.setColor(bgcolor);//给画笔蘸颜色
        g.fillRect(0,0,70,30);

        //生成4个字符画到图片上
        for(int i = 0;i<4;i++){
            //生成一个随机字符
            char c = line.charAt(random.nextInt(line.length()));
            //随机为该字符产生一个颜色
            Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
            g.setColor(color);
            g.setFont(new Font(null, Font.BOLD,20));
            g.drawString(c+"",i*15+5,18+random.nextInt(10)-5);
        }

        //随机生成4条干扰线
        for(int i=0;i<4;i++){
            Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
            g.setColor(color);
            g.drawLine(random.nextInt(71), random.nextInt(31), random.nextInt(71), random.nextInt(31));
        }

        //生成该图片
        try {
            ImageIO.write(image,"jpg",response.getOutputStream());
            response.setContentType("image/jpeg");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        //1创建一张图片，指定宽高
        BufferedImage image = new BufferedImage(70,30, BufferedImage.TYPE_INT_RGB);
        //2从图片上获取一个画笔，目的是通过这个画笔往图片上画东西
        Graphics g = image.getGraphics();
        //3创建一个Random用于产生随机数
        Random random = new Random();
        //图片上允许出现的字符范围(英文和数字)
        String line = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        //随机颜色做背景
        Color bgcolor = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
        g.setColor(bgcolor);//给画笔蘸颜色
        g.fillRect(0,0,70,30);

        //生成4个字符画到图片上
        for(int i = 0;i<4;i++){
            //生成一个随机字符
            char c = line.charAt(random.nextInt(line.length()));
            //随机为该字符产生一个颜色
            Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
            g.setColor(color);
            g.drawString(c+"",i*15+5,18);
        }

        //随机生成4条干扰线
        for(int i=0;i<4;i++){
            Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
            g.setColor(color);
            g.drawLine(random.nextInt(71), random.nextInt(31), random.nextInt(71), random.nextInt(31));
        }

        //生成该图片
        ImageIO.write(image,"jpg",new FileOutputStream("./random.jpg"));
    }

    @RequestMapping("/myweb/createQR")
    public void createQR(HttpServletRequest request, HttpServletResponse response){
        //通过request获取页面表单上提交的数据
        String line = request.getParameter("content");

        //参数1就是二维码内容   参数2生成的图片路径
//        QRCodeUtil.encode(line,"./qr.jpg");
        //参数1:二维码内容   2:二维码中间的logo图  3:生成的图片路径 4:是否压缩logo图的尺寸，保持居中
//        QRCodeUtil.encode(line,"./logo.jpg","./qr.jpg",true);
        //参数1:二维码内容   2:二维码中间的logo图  3:字节输出流，生成的图片会通过该流写出 4:是否压缩logo图的尺寸，保持居中
//        OutputStream out = new FileOutputStream("./qr.jpg");
//        QRCodeUtil.encode(line,"./logo.jpg",out,true);

        OutputStream out = response.getOutputStream();
        try {
            QRCodeUtil.encode(line,"./logo.jpg",out,true);
            response.setContentType("image/jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("生成完毕");
    }
}
